<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class distribute_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    function index()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $d_query = $this->db->get_all("select * from " . table("distribute") . " order by uid desc limit " . ($page - 1) * $page_size . ", " . $page_size);
        if ($d_query) {
            foreach ($d_query as $k => $v) {
                if ($v['uid']) {
                    $user_arr[] = $v['uid'];
                }
                if ($v['from_uid']) {
                    $user_arr[] = $v['from_uid'];
                }
                if ($v['service_uid']) {
                    $user_arr[] = $v['service_uid'];
                }
            }
            $user_arr = array_unique($user_arr);
            $user_in = implode(',', $user_arr);
            $u_query = $this->db->get_all("select * from " . table("member") . " where uid in (" . $user_in . ") limit " . ($page - 1) * $page_size . ", " . $page_size);
            foreach ($u_query as $k => $v) {
                $marr[$v['uid']] = $v['nick'];
            }
            foreach ($d_query as $k => $v) {
                $v['nick'] = @$marr[$v['uid']];
                if (isset($v['from_uid'])) {
                    $v['dis_nick'] = @$marr[$v['from_uid']];
                }
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('distribute'));
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function bill()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $sql = "select d.*, m.uid,m.nick from " . table("distribute_bill") . " as d left join " . table('member') . " as m on d.uid = m.uid ";
        $result = $this->db->get_all($sql);
        if ($result) {
            foreach ($result as $k => $v) {
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('distribute_bill'));
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function rate()
    {
        $data = $this->db->get_all("select * from " . table('distribute_rate') . " order by id asc");
        $this->assign('data', $data);
        $this->display();
    }
    function rate_edit()
    {
        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('distribute_rate') . " where id = " . $id);
            $this->assign('row', $row);
        }
        $this->display();
    }
    function rate_save()
    {
        $setarr['name'] = P('name', '分销名称不能为空');
        $setarr['commission_rate'] = P('commission_rate', '佣金比率不能为空');
        $setarr['px'] = P('px', '', '0');
        $id = P('id', false);
        if ($id) {
            $query = $this->db->update(table('distribute_rate'), $setarr, 'id = ' . $id);
        } else {
            $query = $this->db->add(table('distribute_rate'), $setarr);
        }
        if ($query) {
            right_json();
        } else {
            error_json('保存失败');
        }
    }
    function rate_del()
    {
        $id = $_GET['id'] ? $_GET['id'] : showmsg('提交错误');
        $query = $this->db->delete(table('distribute'), 'id = ' . $id);
        if ($query) {
            showmsg('删除成功');
        } else {
            showmsg('删除失败');
        }
    }
    function service()
    {
        $data = $this->db->get_all("select * from " . table('distribute_service') . " order by id asc");
        $this->assign('data', $data);
        $this->display();
    }
    function service_edit()
    {
        $id = isset($_GET['id']) ? intval($_GET['id']) : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('distribute_service') . " where id = " . $id);
            $this->assign('row', $row);
        }
        $this->display();
    }
    function service_save()
    {
        $setarr['gid'] = P('gid', '产品编号不能为空');
        $setarr['commission_rate'] = P('commission_rate', '提成比率不能为空');
        $id = P('id', 0);
        if ($id) {
            $query = $this->db->update(table('distribute_service'), $setarr, 'id = ' . $id);
        } else {
            $query = $this->db->add(table('distribute_service'), $setarr);
        }
        if ($query) {
            right_json();
        } else {
            error_json('保存失败');
        }
    }
    function service_del()
    {
        $id = $_GET['id'] ? $_GET['id'] : showmsg('提交错误');
        $query = $this->db->delete(table('distribute_service'), 'id = ' . $id);
        if ($query) {
            showmsg('删除成功');
        } else {
            showmsg('删除失败');
        }
    }
    function settlement()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $sql = "select d.*, m.uid,m.nick from " . table("distribute_settle") . " as d left join " . table('member') . " as m on d.uid = m.uid order by addtime desc limit " . ($page - 1) * $page_size . "," . $page_size;
        $result = $this->db->get_all($sql);
        if ($result) {
            foreach ($result as $k => $v) {
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('distribute_settle'));
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function settlement_do()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('操作错误');
        $row = $this->db->get_one("select * from " . table('distribute_settle') . " where id = " . $id);
        if ($row['id']) {
            $user_row = $this->db->get_one("select uid,nick from " . table('member') . " where uid = " . $row['uid']);
            $row['nick'] = @$user_row['nick'];
            $this->assign('row', $row);
        }
        $this->display();
    }
    function settlement_act()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : showmsg('提交错误');
        $settle_month = date("Ym", strtotime("-1 month"));
        $row = $this->db->get_one("select * from " . table('distribute_settle') . " where settle_month = " . $settle_month . " and id = " . $id);
        if ($row['id'] && $row['settle_status'] == 0 && $row['settle_time'] == 0) {
            $this->db->autocommit();
            $query = $this->db->query("update " . table('distribute') . " set price = price+" . $row['price'] . " where uid = " . $row['uid']);
            $sarr['settle_status'] = '1';
            $sarr['settle_time'] = time();
            $query2 = $this->db->update(table('distribute_settle'), $sarr, 'id = ' . $row['id']);
            if ($query && $query2) {
                $this->db->commit();
                showmsg('结算成功', '', '-1');
            } else {
                $this->db->rollback();
                showmsg('结算失败');
            }

        } else {
            showmsg('已经支付或未到结算时间', '', '-1');
        }
    }
}